﻿using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.BIZ;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using FredCK.FCKeditorV2;
using log4net;

namespace AnyOffice.general.news.manage
{
    public partial class edit : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

       
        protected void btnAddFile_Click(object sender, EventArgs e)
        {
            if (this.fileAttach.PostedFile.ContentLength > 0)
            {
                try
                {
                    string uniqueTempPath = this.ViewState["UniqueTempPath"].ToString();
                    if (uniqueTempPath == "")
                    {
                        uniqueTempPath = AttachmentBiz.GetUniqueTempPath();
                        this.ViewState["UniqueTempPath"] = uniqueTempPath;
                    }
                    Upload upload = new Upload();
                    upload.OverwriteFile = true;
                    upload.DestinationPath = uniqueTempPath;
                    ConfigManager manager = new ConfigManager();
                    upload.MaxSize = manager.AttachMaxSize;
                    upload.GetFile(this.fileAttach.PostedFile);
                    upload.DoUpload();
                    this.ViewState["TotalSize"] = Convert.ToInt32(this.ViewState["TotalSize"]) + upload.FileSize;
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    list.Add(Path.GetFileName(this.fileAttach.PostedFile.FileName));
                    this.ViewState["FileList"] = list;
                    this.ViewState["FileNums"] = Convert.ToInt32(this.ViewState["FileNums"].ToString()) + 1;
                    this.ltbFileList.Items.Add(Path.GetFileName(this.fileAttach.PostedFile.FileName));
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) != 0)
                    {
                        this.ltbFileList.Rows = Convert.ToInt32(this.ViewState["FileNums"].ToString());
                    }
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) > 0)
                    {
                        this.pnlAttach.Visible = true;
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("附件添加", exception);
                    }
                }
            }
        }
        protected void btnRemoveFile_Click(object sender, EventArgs e)
        {
            if (this.ltbFileList.Items.Count != 0)
            {
                try
                {
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    list.Remove(this.ltbFileList.SelectedItem.Text);
                    this.ViewState["FileList"] = list;
                    string fileName = this.ViewState["UniqueTempPath"].ToString() + this.ltbFileList.SelectedItem.Text;
                    FileInfo info = new FileInfo(fileName);
                    if (info.Exists)
                    {
                        this.ViewState["TotalSize"] = Convert.ToInt32(this.ViewState["TotalSize"]) - info.Length;
                        if (File.Exists(fileName))
                        {
                            File.Delete(fileName);
                        }
                    }
                    this.ViewState["FileNums"] = Convert.ToInt32(this.ViewState["FileNums"].ToString()) - 1;
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) >= 1)
                    {
                        this.ltbFileList.Rows = Convert.ToInt32(this.ViewState["FileNums"].ToString());
                    }
                    this.ltbFileList.Items.Remove(this.ltbFileList.SelectedItem.Text);
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) == 0)
                    {
                        if (Directory.Exists(this.ViewState["UniqueTempPath"].ToString()))
                        {
                            Directory.Delete(this.ViewState["UniqueTempPath"].ToString());
                        }
                        this.ViewState["UniqueTempPath"] = "";
                        this.pnlAttach.Visible = false;
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string attachmentId = "";

             UserCache cache = new UserCache(base.User.Identity.Name);

            try
            {
                if (this.ViewState["OldAttachmentId"].ToString() != string.Empty)
                {
                    AttachmentBiz.DeleteByAttachmendId(base.Conn, this.ViewState["OldAttachmentId"].ToString());
                }
                if (this.ViewState["UniqueTempPath"].ToString() != "")
                {
                    string uniquePath = AttachmentBiz.GetUniquePath(out attachmentId);
                    Directory.Move(this.ViewState["UniqueTempPath"].ToString(), uniquePath);
                    AttachmentData data = new AttachmentData();
                    AttachmentDao dao = new AttachmentDao();
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    for (int i = 0; i < list.Count; i++)
                    {
                        FileInfo info = new FileInfo(uniquePath + list[i].ToString());
                        if (info.Exists)
                        {
                            data.Type = 5;
                            data.AttachmentId = attachmentId;
                            data.Name = info.Name;
                            data.AddTime = DateTime.Now;
                            data.TotalSize = (int)info.Length;
                            data.UserId = base.User.Identity.Name;
                            dao.Insert(base.Conn, data);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("", exception);
                }
            }
            int num2 = Convert.ToInt32(this.ViewState["NewsId"].ToString());
            NewsData data2 = new NewsData();
            NewsDao dao2 = new NewsDao();
            data2.NewsId = num2;
            if (attachmentId != string.Empty)
            {
                data2.AttachmentId = attachmentId;
            }
            if (this.ToId.Value != string.Empty)
            {
                data2.ToUser = this.ToId.Value;
            }
            //if (this.ToPriv.Value != string.Empty)
            //{
            //    data2.ToPriv = this.ToPriv.Value;
            //}
            //if (this.ToUser.Value != string.Empty)
            //{
            //    data2.ToUser = this.ToUser.Value;
            //} 

            data2.DeptId = cache.DeptId;
          
            data2.Subject = this.txtSubject.Text;
            data2.TypeId = Convert.ToInt32(this.ddlTypeId.SelectedValue);
            data2.FromId = base.User.Identity.Name;
            data2.Content = this.txtContent.Value;
            data2.SendTime = DateTime.Now;
           
            dao2.Update(base.Conn, data2);
            if (this.ckbSms.Checked || this.ckbMobile.Checked)
            {
                SmsData data3 = new SmsData();
                SmsDao dao3 = new SmsDao();
                data3.Type = 14;
                data3.RelateId = num2;
                data3.FromId = base.User.Identity.Name;
                if (this.ToId.Value != string.Empty)
                {
                    data3.ToDeptList = this.ToId.Value;
                }

                if (this.ToId.Value != string.Empty)
                {
                    data3.ToId = this.ToId.Value;
                }

                //if (this.ToPriv.Value != string.Empty)
                //{
                //    data3.ToPrivList = this.ToPriv.Value;
                //}
                //if (this.ToUser.Value != string.Empty)
                //{
                //    data3.ToIdList = this.ToUser.Value;
                //}
                data3.Content = "请查看工作简报！\n标题：" + this.txtSubject.Text;
                data3.RemindFlag = true;
                data3.SendTime = DateTime.Now;
                dao3.BizSend(base.Conn, data3, this.ckbSms.Checked, this.ckbMobile.Checked);
            }
            this.pnlMain.Visible = false;
            this.litMessage.Text = base.ShowPrompt("修改工作简报成功！", "index1.aspx?type_id=" + this.ViewState["TypeId"].ToString() + "&page=" + this.ViewState["CurrentPage"].ToString());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int num = (Request.QueryString["page"] != null) ? int.Parse(Request.QueryString["page"]) : 1;
                this.ViewState["CurrentPage"] = num;
                int newsId = (Request.QueryString["news_id"] != null) ? int.Parse(Request.QueryString["news_id"]) : 0;
                this.ViewState["NewsId"] = newsId;
                int num3 = (Request.QueryString["type_id"] != null) ? int.Parse(Request.QueryString["type_id"].ToString()) : 0;
                this.ViewState["TypeId"] = num3;
                DataTable table = new NewsDao().SelectByNewsId(base.Conn, newsId, base.User.Identity.Name);
                if (table.Rows.Count == 0)
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPrompt("无此工作简报", "index1.aspx?type_id=" + this.ViewState["TypeId"].ToString() + "&page=" + num.ToString());
                }
                else
                {
                    DataTable table2 = new NewsTypeDao().SelectAll(base.Conn);
                    if (table2.Rows.Count > 0)
                    {
                        this.ddlTypeId.DataSource = table2;
                        this.ddlTypeId.DataBind();
                    }
                    if (this.ddlTypeId.Items.FindByValue(table.Rows[0]["TypeId"].ToString()) != null)
                    {
                        this.ddlTypeId.Items.FindByValue(table.Rows[0]["TypeId"].ToString()).Selected = true;
                    }
                    string str = (table.Rows[0]["ToUser"] != DBNull.Value) ? table.Rows[0]["ToUser"].ToString() : "";
                    this.ToId.Value = str;

                    //string str2 = (table.Rows[0]["ToUserNames"] != DBNull.Value) ? table.Rows[0]["ToUserNames"].ToString() : "";
                    //if ((str2 != string.Empty) && (str != "ALL_DEPT"))
                    //{
                    //    str2 = str2 + ",";
                    //}

                    if ((table.Rows[0]["ToUserNames"] != DBNull.Value) && (table.Rows[0]["ToUserNames"].ToString() != string.Empty))
                    {
                        this.ToName.Value = table.Rows[0]["ToUserNames"].ToString() + ",";
                    }
                     
                  //  this.ToName.Value = str2;
                    //if (table.Rows[0]["ToPriv"] != DBNull.Value)
                    //{
                    //    this.ToPriv.Value = table.Rows[0]["ToPriv"].ToString();
                    //}
                    //if ((table.Rows[0]["ToPrivNames"] != DBNull.Value) && (table.Rows[0]["ToPrivNames"].ToString() != string.Empty))
                    //{
                    //    this.ToPrivName.Value = table.Rows[0]["ToPrivNames"].ToString() + ",";
                    //}
                    //if (table.Rows[0]["ToUser"] != DBNull.Value)
                    //{
                    //    this.ToUser.Value = table.Rows[0]["ToUser"].ToString();
                    //}
                  
                    this.txtSubject.Text = table.Rows[0]["Subject"].ToString();
                    if (table.Rows[0]["Content"] != DBNull.Value)
                    {
                        this.txtContent.Value = table.Rows[0]["Content"].ToString();
                    }
                    string attachmentId = (table.Rows[0]["AttachmentId"] != DBNull.Value) ? table.Rows[0]["AttachmentId"].ToString() : "";
                    this.ViewState["FileNums"] = 0;
                    this.ViewState["FileList"] = new ArrayList();
                    this.ViewState["UniqueTempPath"] = "";
                    this.ViewState["TotalSize"] = 0;
                    this.btnAddFile.Attributes.Add("onclick", "return CheckFile();");
                    this.ViewState["OldAttachmentId"] = attachmentId;
                    if (attachmentId != string.Empty)
                    {
                        DataTable table3 = new AttachmentDao().SelectByAttachmentId(base.Conn, attachmentId);
                        if (table3.Rows.Count > 0)
                        {
                            try
                            {
                                this.ViewState["FileNums"] = table3.Rows.Count;
                                ArrayList list = new ArrayList();
                                string savedPath = AttachmentBiz.GetSavedPath(attachmentId);
                                string uniqueTempPath = AttachmentBiz.GetUniqueTempPath();
                                this.ViewState["UniqueTempPath"] = uniqueTempPath;
                                AttachmentBiz.Copy(savedPath, uniqueTempPath);
                                int num4 = 0;
                                for (int i = 0; i < table3.Rows.Count; i++)
                                {
                                    list.Add(table3.Rows[i]["Name"].ToString());
                                    num4 += Convert.ToInt32(table3.Rows[i]["TotalSize"].ToString());
                                }
                                this.ltbFileList.DataSource = list;
                                this.ltbFileList.DataBind();
                                this.ltbFileList.Rows = table3.Rows.Count;
                                this.ViewState["FileList"] = list;
                                this.ViewState["TotalSize"] = num4;
                                this.pnlAttach.Visible = true;
                            }
                            catch (Exception exception)
                            {
                                if (log.IsErrorEnabled)
                                {
                                    log.Error("", exception);
                                }
                            }
                        }
                    }
                    this.btnSubmit.Attributes.Add("onclick", "return CheckForm();");
                    this.ckbMobile.Visible = SmsDao.IsMobile(SmsDao.SmsType.News);
                }
            }
        }
 

    }
}
